<?php
require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/new_smt_variables.php' );

if( !in_array( "N", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){

	if( isset($_GET['shop_id']) && $_GET['shop_id'] != '' ){	
			
		$db = new Class_DB;
		
		if($_GET['mode'] == 'update'){
			
			//SQLクエリー
			$sql = "SELECT * FROM k_shop where shop_id =".$_GET['shop_id'];	
			
			$res = $db->query($sql);
			
			$list = array("shop_id"=>$shop_id);
			
			$cnt = 0;
				
			while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){	
				$row['id']=sprintf('%04d', $row['shop_id']);
				$consumerlist[] = $row;				
				$cnt++;
			}		
			
			$smarty->assign('forms', $consumerlist[0]);
			$smarty->assign('cnt', $cnt);
	
		}elseif ($_GET['mode'] == 'delete'){
			//删除
			//SQLクエリー
			$sql = "DELETE FROM k_shop WHERE shop_id =".$_GET['shop_id'];
			$db->query( $sql );	
			$phase = 'complete';
			$smarty->assign('phase', $phase);
			$smarty->assign('cnt', 1);
		}
	}
}


//登録
if($_SERVER['REQUEST_METHOD'] == 'POST'){
	
	$db = new Class_DB;
	
	if($_POST['mode']=="input"){
		
		$err = new Class_ERROR;
		$err_mes['brand'] = $err->check( $_POST['brand'], array("EXIST") );
		$err_mes['shop_name'] = $err->check( $_POST['shop_name'], array("EXIST") );
		$err_mes['refer_price'] = $err->check( $_POST['refer_price'], array("EXIST","NUM") );
		$err_mes['cost_price'] = $err->check( $_POST['cost_price'], array("EXIST","NUM") );
		$err_mes['s_unit'] = $err->check( $_POST['s_unit'], array("EXIST") );
		
		if( $err->clear ){

			$brand = $_POST['brand'];
			$shop_name = $_POST['shop_name'];
			$size = $_POST['size'];
			$refer_price = $_POST['refer_price'];
			$cost_price = $_POST['cost_price'];
			$s_unit = $_POST['s_unit'];
			
			$date = date('Y/m/d G:i:s');
			
			//SQLクエリー
			$sql = "INSERT INTO k_shop(brand,shop_name,size,refer_price,cost_price,s_unit,r_date,u_date) VALUES('$brand','$shop_name','$size'";
			$sql = $sql.",'".$refer_price."','".$cost_price."','".$s_unit."','".$date."','".$date."')";	
			$db->query( $sql );
			$phase = 'complete';
			$smarty->assign('phase', $phase);
			
		}else{
			
			$smarty->assign('err', $err_mes);
			$smarty->assign('forms', $_POST);
			
		}
		
	}elseif ($_POST['mode']=="update"){
			$err = new Class_ERROR;
			$err_mes['brand'] = $err->check( $_POST['brand'], array("EXIST") );
			$err_mes['shop_name'] = $err->check( $_POST['shop_name'], array("EXIST") );
			$err_mes['refer_price'] = $err->check( $_POST['refer_price'], array("EXIST","NUM") );
			$err_mes['cost_price'] = $err->check( $_POST['cost_price'], array("EXIST","NUM") );
			if($err->clear){
				//更新
				$shop_id = $_POST['shop_id'];
				$brand = $_POST['brand'];
				$shop_name = $_POST['shop_name'];
				$size = $_POST['size'];
				$refer_price = $_POST['refer_price'];
				$cost_price = $_POST['cost_price'];
				$s_unit = $_POST['s_unit'];
				
				$date = date('Y/m/d');
				
				$sql = "UPDATE k_shop SET brand='$brand',shop_name='$shop_name',size='$size',refer_price=$refer_price,cost_price=$cost_price,s_unit='$s_unit',u_date='$date' WHERE shop_id=$shop_id";
				
				$db->query( $sql );
			
				$phase = 'complete';
				$smarty->assign('phase', $phase);
				$smarty->assign('cnt', 1);
		}else{
				$smarty->assign('err', $err_mes);
				$smarty->assign('forms', $_POST);
				$smarty->assign('cnt', 1);
			
		}
		
	}
			
}


$menu_html = file_get_contents( $_SERVER['DOCUMENT_ROOT'].'/templates/web-admin/menu.html' );

$smarty->assign('menu_html', $menu_html);

$smarty->display('web-admin/order_shop_form.html');


?>